package hu.genius.view;

import hu.genius.CoreApplication;
import hu.genius.R;
import hu.genius.model.BasketManager;
import hu.genius.model.DBUtil;
import hu.genius.model.entity.Product;
import hu.genius.view.adapters.ProductItemAdapter;

import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class ProductsListActivity extends ListActivity implements OnClickListener {

	private List<Product> products;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.products);

//		CoreApplication application = (CoreApplication) this.getApplication();
//		BasketManager basketManager = application.getBasketManager();

		Bundle bundle = getIntent().getExtras();
		String categoryName = bundle.getString("category_name");
		int codegoryId = bundle.getInt("category_id");
		String productsLabel = bundle.getString("products_label");

//		Log.i("zs", categoryName + ": " + basketManager.toString());
		products = DBUtil.listProductsByCategory(this, codegoryId);

		TextView productsTextView = (TextView) findViewById(R.id.productsLabelTextView);
		productsTextView.setText(productsLabel + " - " + categoryName);

		Button productsBackButton = (Button) findViewById(R.id.productsBackButton);
		productsBackButton.setOnClickListener(this);

		this.setListAdapter(new ProductItemAdapter(this,
				R.layout.listview_product_item_layout, products));
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);

		Product product = products.get(position);

		Intent intent = new Intent(this, SelectProduct.class);
		intent.putExtra("product_id", product.getId());
		intent.putExtra("product_category_id", product.getCategoryId());
		intent.putExtra("product_name", product.getName());
		intent.putExtra("product_price", product.getPrice());
		intent.putExtra("image", product.getImage());
		
		Log.d("zs", "selected product: (" + product + ")");
		this.startActivity(intent);
	}

	public void onClick(View v) {
		if (v != null) {
			switch (v.getId()) {
			case R.id.productsBackButton: {
				Log.d(this.getClass().getName(), "click back button");
				ProductsListActivity.this.finish();
				break;
			}
			}
		}
	}

}
